<?php
/**
* @package controller
* @file loginController.php
* @date 21.08.2009
* @version 1.0
* @copyright sample
* 
* @todo phpDoc comments
*/
require_once 'Zend/Controller/Action.php';

class User_LoginController extends Cms_Controller_Action
{
	
	public function init()
	{
		//Zend_Layout::getMvcInstance()->setLayoutPath('../app_mvc/index/admin/layouts');
		$this->view->siteTitle = 'sample - Log In';
		
	}
	public function indexAction()    
	{ 
		//print_r($this->_params);
		//echo '<pre>';print_r($_SESSION);exit;
		if(isset($_SESSION['im_userId'])!='') 
		{
			$this->_redirect('/user/profile/display/'.base64_encode(IM_USER_ID));
		}
		 else 
		{
		$key = $this->_getParam('id');
				

		if($key == 'invalid'){
			$message = "Invalid User Name or Password.";
		} else {
			$message='';
		}
		$this->view->message = $message;
		
		}
			
	}
	public function checkLoginAction()
	{
		global $redirectUrls;
		$userName = addslashes($_POST['emailAddress']);
		$password = addslashes($_POST['password']);
		
		$resLogin = Doctrine::getTable('Organizations')->checkLogin($userName,$password);
		//echo $resLogin['org_id'];exit;
		//print "<pre>";
		//print_r($resLogin);
		//exit;
		if($resLogin['org_id'] !='' && $resLogin['org_id'] >0)
		{
			$_SESSION['im_userId'] = $resLogin['org_id'];
			$_SESSION['im_userEmail'] = $resLogin['email_id'];
			$_SESSION['im_userName'] = $resLogin['name_organisation'];
			//$_SESSION['im_userFullName'] = $resLogin['name_organisation'];
			$_SESSION['im_userType']= $resLogin['user_type'];
			define('IM_ADMIN_ID',$_SESSION['im_userType']);
			//$sesval = Doctrine::getTable('User')->UpdateLastLogin($resLogin->email_address,$resLogin->user_id);
			//$_SESSION['sd_sessval']= $sesval;
			
			$id=base64_encode($resLogin['user_id']);
			
			if( !empty($_SESSION['redirectUrl']) ) {
			 
				$this->_redirect($_SESSION['redirectUrl']);
				
			}
			else
			{
				$org_id=base64_encode($resLogin['org_id']);
				$this->_redirect('/home');
			}
			
		} else {
			$this->_redirect('/user/login/index/invalid');
		}
	}
	public function logoutAction()
	{
		session_destroy();
		$this->_redirect('/user/login/');
	}

}
